<?php
/**
 * +----------------------------------------------------------------------
 * | [ DO WHAT YOU LOVE AND FUCK THE REST ]
 * +----------------------------------------------------------------------
 * | EMAIL: < 931274989@qq.com >
 * +----------------------------------------------------------------------
 * | WECHAT: xzc016
 * +----------------------------------------------------------------------
 * | 管理员日志模型
 * +----------------------------------------------------------------------
 */
namespace app\index\model;

use app\common\model\System;

use think\facade\Session;
use think\facade\Request;
use think\facade\Db;

class AdminLog extends Base
{
  //管理员日志记录
  public static function record()
  {
    $is_log = System::where('web','web')->value('is_log');
    // 日志开启
    if ($is_log == 1) {
      //入库信息
      $admin_id   = Session::get('admin.id',0);
      $username   = Session::get('admin.username','Unknown') ;
      $url        = Request::url();
      $title      = '';
      $content    = Request::except(['s','_pjax']); //s 变量为系统内置的变量，_pjax为js的变量，无记录的必要
      $ip         = Request::ip();
      $useragent  = Request::server('HTTP_USER_AGENT');
      $create_time = time();
      
      //标题处理
      $titleArr = self::getBreadCrumb();
      if (is_array($titleArr)) {
        foreach ($titleArr as $k => $v) {
          $title = '[' . $v['title'] . '] -> ' . $title;
        }
        $title = substr($title, 0, strlen($title) - 4);
      }
      
      //内容处理(过长的内容和涉及密码的内容不进行记录)
      if ($content) {
        foreach ($content as $k => $v) {
          if (is_string($v) && strlen($v) > 200 || stripos($k, 'password') !== false) {
            unset($content[$k]);
          }
        }
      }
      
      //插入数据
      if (!empty($title)) {
        self::create([
          'title'       => $title ? $title : '',
          'content'     => !is_scalar($content) ? json_encode($content) : $content,
          'url'         => $url,
          'admin_id'    => $admin_id,
          'username'    => $username,
          'useragent'   => $useragent,
          'ip'          => $ip,
          'create_time' => $create_time
        ]);
      }
    }
    else {
      return true;
    }
    
  }

  /**
   * 获得面包导航
   * @param string $path
   * @return array
   */
  public static function getBreadCrumb($route = '')
  {
    //当前URL
    $route = $route ? $route : Request::controller() . '/' . lcfirst(Request::action());
    //查找名称
    $data = Db::name('auth_rule')->where('name', '=', '/index/'.$route)->find();

    $result = [];
    if ($data) {
        $result[] = [
            'url'   => $data['name'],
            'title' => $data['title'],
            'icon'  => $data['icon'],
        ];
        //查找是否有上级别
        if ($data['pid']) {
            //查询上级url
            $route = Db::name('auth_rule')->where('id', '=', $data['pid'])->find();
            $result = self::getBreadCrumb1($result, $route['name']); 
        }
    } else {
        //不存在的记录
        if ($route == 'Index/index') {
            $result[] = [
                'url'   => 'Index/index',
                'title' => '控制台',
                'icon'  => 'fa fa-dashboard',
            ];
        }
    }
    return $result;
  }

    /**
   * 获得面包导航
   * @param string $path
   * @return array
   */
  public static function getBreadCrumb1($result, $route = '') {
    $data = Db::name('auth_rule')->where('name', '=', $route)->find();
    if ($data) {
      $result[] = [
        'url'   => $data['name'],
        'title' => $data['title'],
        'icon'  => $data['icon'],
      ];
      if ($data['pid']) {
        //查询上级url
        $route = Db::name('auth_rule')->where('id', '=', $data['pid'])->find();
        $result1 = self::getBreadCrumb2($result, $route['name']);
      }
      else {
        foreach ($result as $k => $v) {
          $result1[] = [
              'url'   => $result[$k]['url'],
              'title' => $result[$k]['title'],
              'icon'  => $result[$k]['icon']
          ];
        }
      }
    }
    return $result1;
  }

   /**
   * 获得面包导航
   * @param string $path
   * @return array
   */
  public static function getBreadCrumb2($result, $route = '') {
    $data = Db::name('auth_rule')->where('name', '=', $route)->find();
    if ($data) {
      $result[] = [
        'url'   => $data['name'],
        'title' => $data['title'],
        'icon'  => $data['icon'],
      ];
      foreach ($result as $k => $v) {
        $result2[] = [
            'url'   => $result[$k]['url'],
            'title' => $result[$k]['title'],
            'icon'  => $result[$k]['icon']
        ];
      }
    }
    return $result2;
  }
}